package com.company.UI;

import javax.swing.*;
import javax.swing.border.EmptyBorder;
import javax.swing.border.TitledBorder;
import java.awt.*;
import java.io.DataOutputStream;
import java.net.Socket;

public class PrivateChatUI extends JFrame {
    private String friendName; // 私聊对象的名字
    private Socket socket; // 用于存储发起私聊方的 Socket 对象
    JTextArea chatArea = new JTextArea();

    public PrivateChatUI() {
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setSize(800, 600);
        setLocationRelativeTo(null);
        init();
    }
    public PrivateChatUI(Socket socket, String friendName){
        this();
        setTitle("与 " + friendName + " 的私聊窗口");
        this.socket = socket;
        this.friendName = friendName;
    }



    private void init() {
        // 1) 主面板：自定义渐变背景
        JPanel mainPanel = new JPanel() {
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                Graphics2D g2d = (Graphics2D) g;
                int w = getWidth();
                int h = getHeight();
                // 从上到下的渐变，可根据喜好修改颜色
                GradientPaint gp = new GradientPaint(
                        0, 0, new Color(135, 206, 250), // 浅蓝
                        0, h, new Color(224, 255, 255)  // 浅青
                );
                g2d.setPaint(gp);
                g2d.fillRect(0, 0, w, h);
            }
        };
        mainPanel.setLayout(new BorderLayout());
        setContentPane(mainPanel);

//        // 2) 顶部区域：私聊标题
//        JPanel topPanel = new JPanel(new FlowLayout(FlowLayout.CENTER, 0, 15));
//        topPanel.setOpaque(false); // 让渐变背景可见
//        JLabel titleLabel = new JLabel("与 景哥 的私聊");
//        titleLabel.setFont(new Font("微软雅黑", Font.BOLD, 20));
//        titleLabel.setForeground(Color.WHITE);
//        topPanel.add(titleLabel);
//        mainPanel.add(topPanel, BorderLayout.NORTH);

        // 3) 中间区域：聊天记录
        JPanel centerPanel = new JPanel(new BorderLayout());
        centerPanel.setOpaque(false);
        mainPanel.add(centerPanel, BorderLayout.CENTER);

        // 聊天内容文本域
        chatArea.setEditable(false);
        chatArea.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        // 保持白色底色，方便阅读
        chatArea.setBackground(Color.WHITE);
        chatArea.setBorder(new EmptyBorder(5, 5, 5, 5));

        // 滚动面板 + 标题边框
        JScrollPane chatScrollPane = new JScrollPane(chatArea);
        chatScrollPane.setBorder(BorderFactory.createTitledBorder(
                BorderFactory.createLineBorder(Color.GRAY, 1),
                "聊天记录",
                TitledBorder.LEFT,
                TitledBorder.TOP,
                new Font("微软雅黑", Font.BOLD, 14),
                Color.DARK_GRAY
        ));
        // 让滚动面板半透明显示渐变：可选
        chatScrollPane.setOpaque(false);
        // 保持内部文本域为白底
        chatScrollPane.getViewport().setOpaque(true);

        centerPanel.add(chatScrollPane, BorderLayout.CENTER);

        // 4) 底部区域：输入区 + 发送按钮
        JPanel inputPanel = new JPanel(new BorderLayout());
        inputPanel.setPreferredSize(new Dimension(0, 120));
        inputPanel.setOpaque(false);
        inputPanel.setBorder(BorderFactory.createTitledBorder(
                BorderFactory.createLineBorder(Color.GRAY, 1),
                "输入消息",
                TitledBorder.LEFT,
                TitledBorder.TOP,
                new Font("微软雅黑", Font.BOLD, 14),
                Color.DARK_GRAY
        ));

        // 多行输入框
        JTextArea inputArea = new JTextArea();
        inputArea.setLineWrap(true);
        inputArea.setWrapStyleWord(true);
        inputArea.setMargin(new Insets(5, 5, 5, 5));
        inputArea.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        inputArea.setBackground(Color.WHITE);

        JScrollPane inputScrollPane = new JScrollPane(inputArea);
        inputScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_AS_NEEDED);
        inputScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        inputScrollPane.setOpaque(false);
        inputScrollPane.getViewport().setOpaque(true);
        inputPanel.add(inputScrollPane, BorderLayout.CENTER);

        // 发送按钮
        JButton sendButton = new JButton("发送");
        sendButton.setFont(new Font("微软雅黑", Font.PLAIN, 14));
        sendButton.setPreferredSize(new Dimension(80, 30));
        sendButton.addActionListener(e -> {
            String message = inputArea.getText().trim();
            inputArea.setText("");
            if (message.isEmpty()) {
                JOptionPane.showMessageDialog(this, "请输入要发送的消息！");
                return;
            }
            try {
                DataOutputStream dos = new DataOutputStream(socket.getOutputStream());
                dos.writeInt(3); // 代表发送的是 私聊消息的请求
                dos.writeUTF(friendName);
                dos.writeUTF(message);
                dos.flush();
            } catch (Exception ex) {
                ex.printStackTrace();
            }
        });
        inputPanel.add(sendButton, BorderLayout.EAST);

        centerPanel.add(inputPanel, BorderLayout.SOUTH);

        setVisible(true);
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(PrivateChatUI::new);
    }

    public void showMessages(String privateMessage) {
        chatArea.append(privateMessage + "\n");
    }
}
